package com.zhida.smartcontrol.pagectrl.lampmanager.editlamp;

import android.text.TextUtils;

import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

import java.util.HashMap;
import java.util.Map;

public class EditLampPresenter extends BasePresenterImpl<EditLampContract.View> implements EditLampContract.Presenter {
    private int type;
    private String id;
    private String code;
    private String pid;
    private String address;
    private String equipmentCode;
    private String imei;
    private String lat;
    private String lng;
    private String manufacturer;
    private String name;
    private String num;
    private String switchType;
    private String mDevicePic;

    @Override
    public void check(int type, String code, String pid, String address, String equipmentCode, String imei, String lat,
                      String lng, String manufacturer, String name, String num, String switchType, String filePath, String id) {
        if (TextUtils.isEmpty(switchType)) {
            mView.showMsg("请选择类别");
            return;
        }
        if (TextUtils.isEmpty(name)) {
            mView.showMsg("请输入设备名称");
            return;
        }
        int sType = Integer.parseInt(switchType);
//        添加 NB 单灯和 NB 双灯时，IMEI 码为必填，添加 PLC、ZigBee 单灯时，设 备 ID（code）为必填
        if (sType == Const.NB_DOUBLE || sType == Const.NB_SINGLE) {
            if (TextUtils.isEmpty(imei)) {
                mView.showMsg("请输入设备IMEI码");
                return;
            }
        }
        if (sType == Const.PLC_SINGLE || sType == Const.ZIG_BEE_SINGLE) {
            if (TextUtils.isEmpty(code)) {
                mView.showMsg("设备code不能为空");
                return;
            }
        }
        if (TextUtils.isEmpty(equipmentCode)) {
            mView.showMsg("请输入设备编码");
            return;
        }
        if (TextUtils.isEmpty(num)) {
            mView.showMsg("请输入卡号");
            return;
        }
        if (TextUtils.isEmpty(manufacturer)) {
            mView.showMsg("请输入生产厂家");
            return;
        }
        if (TextUtils.isEmpty(lat) || TextUtils.isEmpty(lng)) {
            mView.showMsg("请选择经纬度");
            return;
        }
        this.type = type;
        this.id = id;
        this.code = code;
        this.pid = pid;
        this.address = address;
        this.equipmentCode = equipmentCode;
        this.imei = imei;
        this.lat = lat;
        this.lng = lng;
        this.manufacturer = manufacturer;
        this.name = name;
        this.num = num;
        this.switchType = switchType;

        if (TextUtils.isEmpty(filePath)) {
            submit();
        } else {
            upLoad(filePath);
        }
    }

    private void upLoad(String filePath) {
        mKalleCenter.upLoad(Api.UPLOAD_PIC, filePath, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mDevicePic = result;
                submit();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    private void submit() {
        if (this.type == Const.TYPE_ADD) {
            addDevice();
        } else {
            editDevice();
        }
    }

    private void addDevice() {
        // 添加单/双灯
        Map<String, Object> params = new HashMap<>();
        params.put("switchType", this.switchType);
        params.put("code", this.code);
        params.put("address", this.address);
        params.put("equipmentCode", this.equipmentCode);
        params.put("num", this.num);
        params.put("name", this.name);
        params.put("imei", this.imei);
        params.put("pid", this.pid);
        params.put("manufacturer", this.manufacturer);
        params.put("devicePic", this.mDevicePic);
        params.put("lng", this.lng);
        params.put("lat", this.lat);
        mKalleCenter.postJson(Api.ADD_LAMP, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    private void editDevice() {
        Map<String, Object> params = new HashMap<>();
        params.put("id", this.id);
        params.put("address", this.address);
        params.put("equipmentCode", this.equipmentCode);
        params.put("lat", this.lat);
        params.put("lng", this.lng);
        params.put("manufacturer", this.manufacturer);
        params.put("name", this.name);
        params.put("num", this.num);
        params.put("devicePic", this.mDevicePic);
        mKalleCenter.putJson(Api.DEVICE_EDIT, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.onSuccess("编辑成功");
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
}
